import sql_gjp as sql
sql = sql.SQL_GJP("127.0.0.1", "root", "111111", "py", 3306)
print("欢迎来到度小满家庭记账系统")
#管理员登录
admin_name = "admin"
admin_password = "123456"
#普通登录
user_name = "lisi"
user_password = "123456"

n = 3
#登录验证
while  n > 0:
    # 登录
    username = input("请输入用户名：")
    password = input("请输入密码：")
    n -= 1
    if admin_name == username and admin_password == password:
        print("欢迎管理员",username)
        break
    elif user_name == username and user_password == password:
        print("欢迎用户",username)
        break
    elif admin_name != username or admin_password != password:
        print("用户名或密码错误，请重新输入")
        print("您还有",n,"次机会")
        print("*"*80)
    elif user_name != username or user_password != password:
        print("用户名或密码错误，请重新输入")
        print("您还有",n,"次机会")
        print("*" * 80)
    else:
        print("请稍后再试")

while admin_name == username and admin_password == password:

    #管理员操作
    print("1.添加账务，2.编辑账务，3.删除账务，4.查询账务，5.退出系统")
    choice = input("请输入您的操作：")
    if choice == "1":
        print("添加账务")
        result = sql.select_all_data("select * from gjp")
        for i in result:
            print(i)
        print("*" * 80)
        print("你要添加多少条账务？")
        num = int(input("请输入数量："))
        if num == 0:
            continue
        else:
            for i in range(num):
                id = int(input("请输入账务ID："))
                type = input("请输入账务类型：")
                account = input("请输入账务账户：")
                money = int(input("请输入账务金额："))
                time = input("请输入账务日期-格式：yyyy-mm-dd：")
                shuoming = input("请输入账务说明：")
                values = (id, type, account, money, time, shuoming)
                result = sql.insert_data(values)
                print("添加成功")
                print("*" * 80)
    elif choice == "2":
        print("编辑账务")
        result = sql.select_all_data("select * from gjp")
        for i in result:
            print(i)
        print("*" * 80)
        # int_id = int(input("请输入要编辑的账务ID："))
        int_id = input("请输入要编辑的账务ID：")
        print("请输入编辑后的信息:")
        type = input("请输入账务类型：")
        account = input("请输入账务账户：")
        money = int(input("请输入账务金额："))
        time = input("请输入账务日期-格式：yyyy-mm-dd：")
        shuoming = input("请输入账务说明：")
        values = (type, account, money, time, shuoming,int_id)
        sql.update_data_by_id(values)
        print("编辑成功")
        print("*" * 80)
    elif choice == "3":
        print("删除账务")
        result = sql.select_all_data("select * from gjp")
        for i in result:
            print(i)
        print("*"*80)
        int_id = int(input("请输入要删除的账务ID："))
        sql.delete_data_by_id(int_id)
        print("删除成功")
        print("*" * 80)
    elif choice == "4":
        print("查询账务")
        result = sql.select_all_data("select * from gjp")
        for i in result:
            print(i)
        print("*"*80)
    elif choice == "5":
        print("退出系统")
        break
    else:
        print("输入错误，请重新输入")

while user_name == username and user_password == password:
    #普通用户操作
    print("1.查询所有，2.按条件查询，3.退出系统")
    choice = input("请输入您的操作：")
    if choice == "1":
        print("查询所有结果：")
        result = sql.select_all_data("select * from gjp")
        for i in result:
            print(i)
        print("*"*80)
    elif choice == "2":
        print("请输入查询条件编号：")
        cho = input("1.按id查询 2.按时间查询:")
        if cho == "1":
            id = input("请输入要查询的账务ID：")
            result = sql.select_data_by_id(id)
            if len(result) == 0:
                print("没有查询到相关数据")
            else:
                print(result)
            print("*" * 80)

        elif cho == "2":
            time = input("请输入要查询的账务时间-格式：yyyy-mm-dd：")
            result = sql.select_data_by_time(time)
            if len(result) == 0:
                print("输入错误，没有查询到相关数据")
            else:
                for i in result:
                    print(i)
            print("*" * 80)

        else:
            print("输入错误，请重新输入")
    elif choice == "3":
        print("退出系统")
        break
    else:
        print("输入错误，请重新输入")

